/*VaWVqaRPOcVLGQLUZRykSxN43lbEnxQvSuDfiMhf4NYKgvGSAU5ZxOpOA4snWHov*/
/**
 * 新增资产审批单 列表态
 * @author cuixuh
 */
import React, { Component } from 'react';
import { createPage, ajax, base, high, createPageIcon } from 'nc-lightapp-front';
import { pageConfig } from './const';
import {
	buttonClick,
	initTemplate,
	searchBtnClick,
	pageInfoClick,
	doubleClick,
	rowSelected,
	commitAction,
	afterEvent,
	setBatchBtnsEnable,
    orgAfterEvent
} from './events';
import { getUrlParamByPrivateUse } from './events/buttonClick';
import ampub from 'ampub';
const { components, utils } = ampub;
const { multiLangUtils, msgUtils, listUtils } = utils;
const { createListTitleArea, getTitleName, creatListSocket } = listUtils;
const { getMultiLangByID } = multiLangUtils;
const { ApprovalTrans } = components;
const { MsgConst, showMessage } = msgUtils;
import Inspection from 'epmp/exports/components/Inspection';
import ApproveDetail from 'uap/common/components/ApproveDetail';
import CreateNCUploade from 'uap/common/components/CreateNCUploade';

// 参照过滤业务单元
const { components: { assetOrgMultiRefFilter: { renderCompleteEvent } } } = ampub;


const {createNCUploader, show: ncUploaderShow, close: ncUploaderClose} = CreateNCUploade;

const { NCAffix, NCDiv } = base;
const {
	searchAreaId,
	tableId,
	pkNtbparadimvo,
	url,
	pagecode,
	pkField,
	bill_type,
	title,
	dataSource,
	alarmId,
	alarmTitle
} = pageConfig;
import '../index.less';

class List extends Component {
	constructor(props) {
		super(props);

		props.use.search(searchAreaId);
		props.use.table(tableId);

		this.state = {
			show: false,
			sourceData: null,
			showApprove: false,
			transi_type: '',
			pk_newasset: '',
			compositedisplay: false,
			compositedata: {},
			uploader: {}
		};
		initTemplate.call(this, props);
	}

	componentDidMount() {
		let pk_ntbparadimvo = getUrlParamByPrivateUse.call(this, pkNtbparadimvo);
		//判断是否是通过预算联查跳转过来的页面
		if (pk_ntbparadimvo) {
			this.getData(pk_ntbparadimvo);
		}
	}

	getData = (pk_ntbparadimvo) => {
		var _this = this;
		ajax({
			url: url.QUERYABOUTBUDGETLIST,
			data: {
				pk_ntbparadimvo,
				pageCode: pagecode
			},
			success: (res) => {
				if (res.data) {
					_this.props.table.setAllTableData(tableId, res.data[tableId]);
				} else {
					//返回的数据为空
					_this.props.table.setAllTableData(tableId, { rows: [] });
					showMessage.call(this, _this.props, { type: MsgConst.Type.QueryNoData });
				}
			}
		});
	};

	//提交及指派 回调
	getAssginUsedr = (value) => {
		commitAction.call(this, this.props, 'SAVE', null, value);
	};
	//取消 指派
	turnOff = () => {
		this.setState({
			compositedisplay: false
		});
	};

	render() {
		let { table, search, button, ncmodal, modal } = this.props;
		let { createModal } = ncmodal;
		let { createSimpleTable } = table;
		let { NCCreateSearch } = search;
		let { createButtonApp } = button;

		return (
			<div className="nc-bill-list">
				<NCAffix>
					<NCDiv areaCode={NCDiv.config.HEADER} className="nc-bill-header-area">
						{/* 标题 title */}
						<div className="header-title-search-area">
							{createListTitleArea.call(this, this.props, {
								title: getTitleName.call(this, this.props) /* 国际化处理： 新增资产审批单*/
							})}
						</div>
						{/* 按钮区 btn-area */}
						<div className="header-button-area">
							{createButtonApp({
								area: 'list_head',
								buttonLimit: 3,
								onButtonClick: buttonClick.bind(this),
								popContainer: document.querySelector('.header-button-area')
							})}
						</div>
					</NCDiv>
				</NCAffix>
				{/* 查询区 search-area */}
				<div className="nc-bill-search-area">
					{NCCreateSearch(searchAreaId, {
						onAfterEvent: afterEvent.bind(this),
						clickSearchBtn: searchBtnClick.bind(this),
						dataSource: dataSource,
						componentInitFinished: () => {
							//缓存数据赋值成功的钩子函数
							//若初始化数据后需要对数据做修改，可以在这里处理
						},// 查询区渲染后：主组织关联参照字段控制其业务单元是否显示
						renderCompleteEvent: renderCompleteEvent.bind(
							this,
							this.props,
							searchAreaId,
							'pk_org',
							orgAfterEvent.bind(this)
						),
                        // 优先级低于clickAdvBtnEve
                        statusChangeEvent: renderCompleteEvent.bind(
							this,
							this.props,
							searchAreaId,
							'pk_org',
							orgAfterEvent.bind(this)
						)
					})}
				</div>
				{/* 列表区 table-area */}
				<div className="nc-bill-table-area">
					{createSimpleTable(tableId, {
						showIndex: true,
						showCheck: true,
						onRowDoubleClick: doubleClick.bind(this),
						onSelected: rowSelected.bind(this),
						onSelectedAll: rowSelected.bind(this),
						handlePageInfoChange: pageInfoClick.bind(this),
						dataSource: dataSource,
						pkname: pkField,
						crossPageSelect: false,
						componentInitFinished: () => {
							//缓存数据赋值成功的钩子函数
							//若初始化数据后需要对数据做修改，可以在这里处理
							setBatchBtnsEnable.call(this, this.props, tableId);
						}
					})}
				</div>
				{/* 确认取消框 */}
				{createModal('confirm', { color: 'warning' })}
				{/* 预警提示框 */}
				{modal.createModal(alarmId, {
					title: getMultiLangByID(alarmTitle) /* 国际化处理： 预警提示*/
				})}

				<Inspection
					show={this.state.show}
					sourceData={this.state.sourceData}
					cancel={() => {
						this.setState({ show: false });
					}}
					affirm={() => {
						this.setState({ show: false });
					}}
				/>

				{/*审批详情*/}
				<ApproveDetail
					show={this.state.showApprove}
					close={() => {
						this.setState({ showApprove: false });
					}}
					billtype={this.state.transi_type}
					billid={this.state.pk_newasset}
				/>

				{/*附件*/}
				{createNCUploader.call(this, `${pagecode}-uploader`, {})}

				{/* 提交及指派 */}
				{this.state.compositedisplay ? (
					<ApprovalTrans
						title={getMultiLangByID('amcommon-000002')} /* 国际化处理： 指派*/
						data={this.state.compositedata}
						display={this.state.compositedisplay}
						getResult={this.getAssginUsedr}
						cancel={this.turnOff}
					/>
				) : (
					''
				)}


				{/* 创建 socket 链接 */}
				{creatListSocket(this.props, {
						tableAreaCode: tableId,//表格区域编码
						billpkname: pkField,//表格主键
						billtype: bill_type,//单据类型,用于查询追溯
					}
				)}
			</div>
		);
	}
}

List = createPage({})(List);

export default List;

/*VaWVqaRPOcVLGQLUZRykSxN43lbEnxQvSuDfiMhf4NYKgvGSAU5ZxOpOA4snWHov*/